package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.frogs.system.util.UserMessageStatusEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.component.SmsHelper;
import com.frogs.ucenter.v1.request.SmsCheckRequest;
import com.frogs.ucenter.v1.request.SmsReceiveRequest;
import com.frogs.ucenter.v1.request.SmsVerifyRequest;
import com.frogs.ucenter.v1.response.DefaultResponse;
import com.frogs.ucenter.v1.service.SmsService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Service(ApiConstants.VERSION + "smsService")
public class SmsServiceImpl implements SmsService {
	
	private static final Logger log = LoggerFactory.getLogger(SmsServiceImpl.class);
	
	@Resource
	private SmsHelper smsHelper;

	@Override
	public Response sendMessage(Request request) throws Exception {
		Response response = new DefaultResponse();
		try {
			SmsReceiveRequest smsReceiveRequest = (SmsReceiveRequest) request;
			response = smsHelper.sendMessage(smsReceiveRequest.getPhoneNumber(), smsReceiveRequest.getMessageContent(),
					smsReceiveRequest.getSendLevel(), smsReceiveRequest.getPartner());
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
	
	/**
	 * 验证码发送
	 */
	@Override
	public Response sendVerify(Request request) throws Exception{
		Response response = new DefaultResponse();
		try {
			SmsVerifyRequest smsVerifyRequest = (SmsVerifyRequest)request;
			response = smsHelper.sendVerify(smsVerifyRequest.getPhoneNumber(), smsVerifyRequest.getOperation(),
					smsVerifyRequest.getCaptchaValid(), smsVerifyRequest.getApplicablePlatformId(), smsVerifyRequest.getPartner());
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
	
	/**
	 * 验证码检验
	 */
	@Override
	public Response checkVerify(Request request) throws Exception{
		Response response = new DefaultResponse();
		try {
			SmsCheckRequest smsCheckRequest=(SmsCheckRequest)request;
			response =smsHelper.checkVerify(smsCheckRequest.getPhoneNumber(), smsCheckRequest.getVerificationCode(), 
					smsCheckRequest.getOperation(), smsCheckRequest.getApplicablePlatformId(), UserMessageStatusEnum.VERIFY.getId());
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
}
